package com.example.redis.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.Netval;
import com.example.mapper.NetvalMapper;
import com.example.redis.dto.FundSearchDto;
import com.example.redis.service.IFundWarmUpService;
import com.example.vo.FundVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FundWarmUpServiceImpl implements IFundWarmUpService {

    @Autowired
    private NetvalMapper netvalMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public FundSearchDto getAllFund(String sortField, String sortDirection) {

        String fundListStr = (String) redisTemplate.opsForHash().get("fundList", "allFundBy" + sortField + sortDirection);
        ObjectMapper objectMapper = new ObjectMapper();
        List<String> fundList = null;
        try {
            fundList = objectMapper.readValue(fundListStr, new TypeReference<List<String>>() {});
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

//        if (StringUtils.isEmpty(fundList)|| fundList.size()==1){
//            fundList = netvalMapper.selectFundList();
//        }
        if (StringUtils.isEmpty(fundListStr)|| fundListStr==""){
            fundList = netvalMapper.selectFundList(sortField,sortDirection);
        }
        String voListO = (String) redisTemplate.opsForHash().get("voList", "allFundBy" + sortField + sortDirection);
//        ObjectMapper objectMapper = new ObjectMapper();
        List<FundVO> voList = new ArrayList<>();
        if (voListO!=null) {
            try {
                voList = objectMapper.readValue(voListO, new TypeReference<List<FundVO>>() {});
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }

        if (StringUtils.isEmpty(voListO)||voListO==""&&StringUtils.isEmpty(voList)||voList.size()==0){
            Calendar calendar = Calendar.getInstance();

            //当前的时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date nowDate = new Date();
            String now = sdf.format(nowDate);

            //近一周
            calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)-7);//7天前
            Date day = calendar.getTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String oneWeek = format.format(day);

            //近一个月
            calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+7);
            calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);//一个月前
            Date day2 = calendar.getTime();
            String oneMonth = format.format(day2);

            //近三个月
            calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-2);//1+2 =3个月前
            Date day3 = calendar.getTime();
            String threeMonth = format.format(day3);

            //近一年
            calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)+3);
            calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)-1);//
            Date day4 = calendar.getTime();
            String oneYear = format.format(day4);

            //近两年
            calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)-1);//
            Date day5 = calendar.getTime();
            String twoYear = format.format(day5);

            //近三年
            calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)-1);//
            Date day6 = calendar.getTime();
            String threeYear = format.format(day6);

            //今年来
            calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR)+3);//
            calendar.clear(Calendar.MONTH);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            Date day7 = calendar.getTime();
            String thisYear = format.format(day7);

            for (String s : fundList) {
                QueryWrapper<Netval> netvalQueryWrapper = new QueryWrapper<>();

                netvalQueryWrapper.eq("fund_short_name",s);

                netvalQueryWrapper.orderByDesc(sortField);

                netvalQueryWrapper.last("limit 1");
                //成立来
                Netval netval = netvalMapper.selectOne(netvalQueryWrapper);
                Date endDate = netval.getEndDate();
                String since = sdf.format(endDate);

                List<FundVO> voList1 = netvalMapper.fundVal(now,oneWeek,oneMonth,threeMonth,oneYear,twoYear,threeYear,thisYear,since,s);
                FundVO fundVO = voList1.get(0);
                FundVO vo = new FundVO();
                vo.setFundCode(fundVO.getFundCode());
                vo.setFundShortName(fundVO.getFundShortName());
                vo.setEndDate(fundVO.getEndDate());
                vo.setUnitNetVal(fundVO.getUnitNetVal());
                if (fundVO.getOneWeekNearly()!=null){
                    vo.setOneWeekNearly(fundVO.getOneWeekNearly());
                }else {
                    vo.setOneWeekNearly(fundVO.getOneMonthNearly());
                }

                if (fundVO.getOneMonthNearly()!=null){
                    vo.setOneMonthNearly(fundVO.getOneMonthNearly());
                }else {
                    vo.setOneMonthNearly(fundVO.getThreeMonthNearly());
                }

                if (fundVO.getThreeMonthNearly()!=null){
                    vo.setThreeMonthNearly(fundVO.getThreeMonthNearly());
                }else {
                    vo.setThreeMonthNearly(fundVO.getOneYearNearly());
                }

                if (fundVO.getOneYearNearly()!=null){
                    vo.setOneYearNearly(fundVO.getOneYearNearly());
                }else {
                    vo.setOneYearNearly(fundVO.getTwoYearNearly());
                }

                if (fundVO.getTwoYearNearly()!=null){
                    vo.setTwoYearNearly(fundVO.getTwoYearNearly());
                }else {
                    vo.setTwoYearNearly(fundVO.getThreeYearNearly());
                }

                if (fundVO.getThreeYearNearly()!=null){
                    vo.setThreeYearNearly(fundVO.getThreeYearNearly());
                }else {
                    vo.setThreeYearNearly(fundVO.getSinceSetUp());
                }

                if (fundVO.getThisYearNearly()!=null){
                    vo.setThisYearNearly(fundVO.getThisYearNearly());
                }else {
                    vo.setThisYearNearly(fundVO.getOneYearNearly());
                }
                vo.setSinceSetUp(fundVO.getSinceSetUp());

                voList.add(vo);
            }
        }

        if (StringUtils.isEmpty(fundListStr)||fundListStr==""){
            redisTemplate.opsForHash().put("fundList","allFundBy" + sortField + sortDirection, JSONArray.toJSONString(fundList) );
        }
        if (StringUtils.isEmpty(voListO)||voListO==""){
            redisTemplate.opsForHash().put("voList","allFundBy" + sortField + sortDirection,JSONArray.toJSONString(voList) );
        }
        System.err.println(fundList);
        return new FundSearchDto(fundList,voList);
    }
}
